import { Component, ChangeDetectorRef } from "@angular/core";
import {
  NavController,
  NavParams,
  ToastController,
  AlertController
} from "ionic-angular";
import { ApplyRepairBean } from "./bean/apply-repair.bean";
import { HttpService } from "../../providers/http.service";
import * as moment from "moment";
import { Storage } from "@ionic/storage";
import { HomePage } from "./../home/home";
import { CommonFunc } from "../../providers/commonFunc";
import { RepairInfoPage } from "../repair-info/repair-info";
@Component({
  selector: "page-repair-apply-detail",
  templateUrl: "apply-repair-detail.html"
})
export class ApplyRepairDetailPage {
  private applyRepairBean: ApplyRepairBean = new ApplyRepairBean();
  dtype = "sp";
  imgsrc: any;
  spimgsrc: any;
  spqm: boolean = false;
  title: string = '报';
  outSideNum :string = ""; // 电梯外联码
  approveUserId : any;
  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    public httpService: HttpService,
    private storage: Storage,
    private toast: ToastController,
    private alertCtrl: AlertController,
    private commonFunc: CommonFunc,
    public cd: ChangeDetectorRef
  ) {

    this.storage.get('user').then(data=>{
        this.approveUserId = data.id;
    });

    if (this.navParams.get("dtype")) {
      this.dtype = this.navParams.get("dtype");
    }
    let id = this.navParams.get("id");
    this.httpService.queryAppliyRepair(id).subscribe(data => {
      this.httpService.getElevatorInfoByNum({"elevatorNum":data.data.elevatorNum}).subscribe(data=>{
          if (data.status == 200) {
            this.outSideNum = data.data.outsideNum;
          }
      })
      this.applyRepairBean = data.data;
      if (this.applyRepairBean.applyType == "2") {
        this.title = '召';
      } else {
        this.title = '报';
      }
      this.applyRepairBean.repairTime = this.commonFunc.transformtime(
        this.applyRepairBean.repairTime
      );
      this.applyRepairBean.applyTime = this.commonFunc.transformtime(
        this.applyRepairBean.applyTime
      );
      if (this.applyRepairBean.applyType == "1") {
        this.imgsrc = this.applyRepairBean.maintenanceUsedUrl;
        if (
          this.applyRepairBean.usedUserUrl != null &&
          this.applyRepairBean.usedUserUrl != ""
        ) {
          this.spqm = true;
        }
        if (this.dtype == "wx" || this.dtype == "lk" || this.spqm) {
          this.spimgsrc = this.applyRepairBean.usedUserUrl;
        }
      } else {
        this.imgsrc = this.applyRepairBean.usedUserUrl;
        if (
          this.applyRepairBean.maintenanceUsedUrl != null &&
          this.applyRepairBean.maintenanceUsedUrl != ""
        ) {
          this.spqm = true;
        }
        if (this.dtype == "wx" || this.dtype == "lk" || this.spqm) {
          this.spimgsrc = this.applyRepairBean.maintenanceUsedUrl;
        }
      }
      this.cd.detectChanges();
    });
  }

  send(state) {

    if (this.applyRepairBean.applyType == "1") {
      if (!this.applyRepairBean.usedUserUrl) {
        const alert = this.alertCtrl.create({
          subTitle: "请签名",
          buttons: ["确定"]
        });
        alert.present();
        return;
      }
    }
    if (this.applyRepairBean.applyType == "2") {
      if (!this.applyRepairBean.maintenanceUsedUrl) {
        const alert = this.alertCtrl.create({
          subTitle: "请签名",
          buttons: ["确定"]
        });
        alert.present();
        return;
      }
    }
    this.applyRepairBean.approveUserId = this.approveUserId;
    this.applyRepairBean.approveStatus = state;
    if (state == "2") {
      if (!this.applyRepairBean.refuseReason) {
        const alert = this.alertCtrl.create({
          subTitle: "请填写您的拒绝原因在(审批意见)项中",
          buttons: ["确定"]
        });
        alert.present();
        return;
      }
    }
    // this.applyRepairBean.dealTime = moment(new Date()).format("YYYYMMDDHHmmss");
    this.applyRepairBean.repairTime = this.applyRepairBean.repairTime
      .replace(/-/g, "")
      .replace(/T/g, "")
      .replace(/Z/g, "")
      .replace(/:/g, "")
      .replace(" ", "");
    this.applyRepairBean.applyTime = this.applyRepairBean.applyTime
      .replace(/-/g, "")
      .replace(/T/g, "")
      .replace(/Z/g, "")
      .replace(/:/g, "")
      .replace(" ", "");
    // this.applyRepairBean.dealStatus = '1';

    this.httpService.updateRepairApply(this.applyRepairBean).subscribe(data => {
      if (data.status == 200) {
        //更新消息状态
        this.httpService
          .updateReadState({ id: this.navParams.get("noticeId"), isRead: "1" })
          .subscribe(data => {
            if (data.status == 200) {
              let tt = this.toast.create({
                message: "提交成功",
                duration: 2000,
                position: "top"
              });
              tt.present().then(() => {
                this.navCtrl.setRoot(HomePage).then(() => {
                  window["jQuery"]("#signatureid").remove();
                  window["jQuery"]("#showImage").remove();
                  this.navCtrl
                    .getActive()
                    .getNav()
                    .remove(0);
                });
              });
            } else {
              const alert1 = this.alertCtrl.create({
                subTitle: "更新消息状态失败!",
                buttons: ["确定"]
              });
              alert1.present();
            }
          });
      } else {
        const alert = this.alertCtrl.create({
          // title: '',
          subTitle: "提交失败",
          buttons: ["确定"]
        });
        alert.present();
      }
    });
  }

  getImgUrl(param) {
    if (this.applyRepairBean.applyType == "1") {
      this.applyRepairBean.usedUserUrl = param.imageDate;
    }
    if (this.applyRepairBean.applyType == "2") {
      this.applyRepairBean.maintenanceUsedUrl = param.imageDate;
    }
  }

  towx() {
    this.navCtrl.push(RepairInfoPage, this.applyRepairBean);
  }

  updateRead() {
    this.httpService
      .updateReadState({ id: this.navParams.get("noticeId"), isRead: "1" })
      .subscribe(data => {
        if (data.status == 200) {
          let tt = this.toast.create({
            message: "提交成功",
            duration: 2000,
            position: "top"
          });
          tt.present().then(() => {
            this.navCtrl.setRoot(HomePage).then(() => {
              window["jQuery"]("#signatureid").remove();
              window["jQuery"]("#showImage").remove();
              this.navCtrl
                .getActive()
                .getNav()
                .remove(0);
            });
          });
        } else {
          const alert1 = this.alertCtrl.create({
            subTitle: "更新消息状态失败!",
            buttons: ["确定"]
          });
          alert1.present();
        }
      });
  }
}
